<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width,initial-scale=1"><title>MyCat概述与基本概念 | 王开琦的个人小站</title><meta name="keywords" content="读写分离,分库分表,分布式数据库系统"><meta name="author" content="王开琦"><meta name="copyright" content="王开琦"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="开源的分布式数据库系统，前端的用户可以把它看成是一个数据库代理，用MySql客户端和命令行工具都可以访问，而其后端则是用MySql原生的协议与多个MySql服务之间进行通信。MyCat的核心功能是分库分表。">
<meta property="og:type" content="article">
<meta property="og:title" content="MyCat概述与基本概念">
<meta property="og:url" content="https://blog.wkq.pub/blog/15-%E5%88%86%E5%B8%83%E5%BC%8F%E6%9E%B6%E6%9E%84-%E8%AF%BB%E5%86%99%E5%88%86%E7%A6%BB%E5%92%8C%E5%88%86%E5%BA%93%E5%88%86%E8%A1%A8-MyCat%E8%AF%BB%E5%86%99%E5%88%86%E7%A6%BB%E3%80%81%E5%88%86%E5%BA%93%E5%88%86%E8%A1%A8/index.html">
<meta property="og:site_name" content="王开琦的个人小站">
<meta property="og:description" content="开源的分布式数据库系统，前端的用户可以把它看成是一个数据库代理，用MySql客户端和命令行工具都可以访问，而其后端则是用MySql原生的协议与多个MySql服务之间进行通信。MyCat的核心功能是分库分表。">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://blog.wkq.pub/media/top_img.jpg">
<meta property="article:published_time" content="2022-04-11T10:17:11.000Z">
<meta property="article:modified_time" content="2022-04-13T12:35:05.004Z">
<meta property="article:author" content="王开琦">
<meta property="article:tag" content="读写分离">
<meta property="article:tag" content="分库分表">
<meta property="article:tag" content="分布式数据库系统">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://blog.wkq.pub/media/top_img.jpg"><link rel="shortcut icon" href="/media/favicon.ico"><link rel="canonical" href="https://blog.wkq.pub/blog/15-%E5%88%86%E5%B8%83%E5%BC%8F%E6%9E%B6%E6%9E%84-%E8%AF%BB%E5%86%99%E5%88%86%E7%A6%BB%E5%92%8C%E5%88%86%E5%BA%93%E5%88%86%E8%A1%A8-MyCat%E8%AF%BB%E5%86%99%E5%88%86%E7%A6%BB%E3%80%81%E5%88%86%E5%BA%93%E5%88%86%E8%A1%A8/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: {"path":"search.xml","languages":{"hits_empty":"找不到您查询的内容：${query}"}},
  translate: {"defaultEncoding":2,"translateDelay":0,"msgToTraditionalChinese":"繁","msgToSimplifiedChinese":"簡"},
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '天',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    jQuery: 'https://cdn.jsdelivr.net/npm/jquery@latest/dist/jquery.min.js',
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/js/jquery.justifiedGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/css/justifiedGallery.min.css'
    },
    fancybox: {
      js: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.js',
      css: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.css'
    }
  },
  isPhotoFigcaption: true,
  islazyload: true,
  isanchor: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: 'MyCat概述与基本概念',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2022-04-13 20:35:05'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if (GLOBAL_CONFIG_SITE.isHome && /iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    document.addEventListener('pjax:complete', detectApple)})(window)</script><meta name="generator" content="Hexo 5.2.0"></head><body><div id="loading-box"><div class="loading-left-bg"></div><div class="loading-right-bg"></div><div class="spinner-box"><div class="configure-border-1"><div class="configure-core"></div></div><div class="configure-border-2"><div class="configure-core"></div></div><div class="loading-word">加载中...</div></div></div><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src= "" data-lazy-src="/media/avatar.jpg" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="site-data"><div class="data-item is-center"><div class="data-item-link"><a href="/archives/"><div class="headline">文章</div><div class="length-num">15</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/tags/"><div class="headline">标签</div><div class="length-num">17</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/categories/"><div class="headline">分类</div><div class="length-num">15</div></a></div></div></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 主页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('/media/top_img.jpg')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/">王开琦的个人小站</a></span><div id="menus"><div id="search-button"><a class="site-page social-icon search"><i class="fas fa-search fa-fw"></i><span> 搜索</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 主页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">MyCat概述与基本概念</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2022-04-11T10:17:11.000Z" title="发表于 2022-04-11 18:17:11">2022-04-11</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2022-04-13T12:35:05.004Z" title="更新于 2022-04-13 20:35:05">2022-04-13</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/%E5%88%86%E5%B8%83%E5%BC%8F%E6%9E%B6%E6%9E%84/">分布式架构</a><i class="fas fa-angle-right post-meta-separator"></i><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/%E5%88%86%E5%B8%83%E5%BC%8F%E6%9E%B6%E6%9E%84/%E8%AF%BB%E5%86%99%E5%88%86%E7%A6%BB%E5%92%8C%E5%88%86%E5%BA%93%E5%88%86%E8%A1%A8/">读写分离和分库分表</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-wordcount"><i class="far fa-file-word fa-fw post-meta-icon"></i><span class="post-meta-label">字数总计:</span><span class="word-count">5.1k</span><span class="post-meta-separator">|</span><i class="far fa-clock fa-fw post-meta-icon"></i><span class="post-meta-label">阅读时长:</span><span>19分钟</span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h2 id="MyCat概述与基本概念"><a href="#MyCat概述与基本概念" class="headerlink" title="MyCat概述与基本概念"></a>MyCat概述与基本概念</h2><p><code>MyCat</code>是什么？从定义和分类来看，它是一个开源的分布式数据库系统，前端的用户可以把它看成是一个数据库代理，用<code>MySql</code>客户端和命令行工具都可以访问，而其后端则是用<code>MySql</code>原生的协议与多个<code>MySql</code>服务之间进行通信。</p>
<p><code>MyCat</code>的核心功能是分库分表，即将一个大表水平切分成<code>N</code>个小表，然后存放在后端的<code>MySql</code>数据库当中。</p>
<p><code>MyCat</code>发展到目前的版本，已经不是一个单纯的<code>MySql</code>代理了，它的后端支持<code>MySql</code>，<code>Oracle</code>，<code>SqlServer</code>，<code>DB2</code>等主流的数据库，也支持<code>MongoDB</code>这种<code>NoSql</code>据库。而对于前端的用户来说，无论后端采用哪一种数据库，在<code>MyCat</code>里都是一个传统的数据库，支持标准的<code>SQL</code>语句,对于前端开发人员来说，可以大大降低开发难度，提升开发速度。</p>
<div class="note info modern"><p>对于DBA来说，可以这样理解MyCat:</p>
</div>
<font color="red">MyCat就是MySql，而MyCat后面连接的MySql，可以理解为MySql中的存储引擎，比如：MyISAM、InnoDB等。所以，MyCat本身不存储数据，数据都是存储在MyCat后面连接的MySql上，数据的可靠性和事务都是MySql保证的。</font>

<div class="note info modern"><p>对于开发人员，可以这样理解MyCat:</p>
</div>
<font color="red">MyCat就是一个近似等于MySql的数据库服务，你可以使用连接MySql的方式连接MyCat。绝大多数情况，你也可以使用常用的ORM框架连接MyCat，但是，对于分片的表，还是建议使用标准SQL语句，这样能够达到最佳的性能。</font>

<div class="note info modern"><p>对于架构师，可以这样理解MyCat:</p>
</div>
<font color="red">MyCat是一个强大的数据库中间件，不仅仅用作读写分离、分库分表，还可以用作容灾备份，云平台建设等，让你的架构具备很强的适应性和灵活性。</font>

<p><img src= "" data-lazy-src="MyCat架构图.png" alt=""></p>
<h2 id="MyCat的应用场景"><a href="#MyCat的应用场景" class="headerlink" title="MyCat的应用场景"></a>MyCat的应用场景</h2><p>MyCat发展到现在，使用的场景很丰富，常见的典型的应用场景有：</p>
<ul>
<li>单纯的读写分离，此时配置最为简单，支持读写分离，主从切换；</li>
<li>分库分表，对于超过1000w的表进行分片，最大支持1000亿的数据；</li>
<li>多租户应用，每个应用一个数据库，应用只连接MyCat，程序本身不需要改造；</li>
<li>代替HBase，分析大数据；</li>
</ul>
<h2 id="MyCat中的基本概念"><a href="#MyCat中的基本概念" class="headerlink" title="MyCat中的基本概念"></a>MyCat中的基本概念</h2><p>MyCat是一个数据库中间件，介于应用和数据库之间，是进行数据处理和交互的中间服务。</p>
<h3 id="逻辑库（Schema）"><a href="#逻辑库（Schema）" class="headerlink" title="逻辑库（Schema）"></a>逻辑库（Schema）</h3><p>在实际的开发中，开发人员不需要知道数据库中间件的存在，开发人员只需要知道有数据库的概念就可以了。所以<strong>数据库中间件可以被看作是一个或者多个数据库集群构成的逻辑库</strong>。例如：上图中例子，可以理解为系统先做了垂直切分，被分为了3个库，用户库、订单库、商品库，而这三个库就被称为逻辑库。</p>
<h3 id="逻辑表（table）"><a href="#逻辑表（table）" class="headerlink" title="逻辑表（table）"></a>逻辑表（table）</h3><p>既然有逻辑库，那么就有逻辑表，对于应用系统来说，读写数据的表，就是逻辑表。而逻辑表中的数据，则是水平切分后，分布在不同的分片库中。如上图所示：假设用户库中有一张用户表，这个用户表就被称为逻辑表，而用户表又被水平切分为3个表，每一个表中都存储一部分用户数据。业务系统在进行用户数据的读写时，只需要操作逻辑表就可以了，后面的分片细节则由MyCat进行操作，这些对于业务系统的开发人员来说是完全透明的。当然，有些表的数据没有那么大，完全不需要分片，只在一个物理的数据库表中即可。</p>
<p>凡是做数据水平切分的表，叫做<strong>分片表</strong>。而数据量比较小，没有进行分片的表，叫<strong>非分片表</strong>。</p>
<p>在真实的业务系统中，往往存在着大量的字典表，这些表的数据基本上很少变动，比如：订单状态。我们在查询的时候，往往需要关联字典表去查询，比如：查询订单时，需要把订单状态关联查出，如果订单表做了分片，分片在不同的数据库中，而订单状态表由于数据量小，没有做分片，那么我们查询的时候就要跨库关联查询订单状态，增加了不必要的麻烦，不如我们干脆把订单状态表冗余到所有订单分片库中，这样关联查询就不需要跨库了，我们把这种通过数据冗余方式复制到所有分片库中的表，叫做<strong>全局表</strong>。</p>
<h3 id="分片节点（dataNode）"><a href="#分片节点（dataNode）" class="headerlink" title="分片节点（dataNode）"></a>分片节点（dataNode）</h3><p>数据被切分后，一张大表被分到不同的分片数据库上面，每个分片表所在的数据库就叫做<strong>分片节点</strong></p>
<h3 id="节点主机（dataHost）"><a href="#节点主机（dataHost）" class="headerlink" title="节点主机（dataHost）"></a>节点主机（dataHost）</h3><p>数据切分后，每一个分片节点不一定都会占用一个真正的物理主机，会存在多个分片节点在同一个物理主机上的情况，这些分片节点所在的主机就叫做<strong>节点主机</strong>。为了避免单节点并发数的限制，尽量将读写压力高的分片节点放在不同的节点主机上。</p>
<h3 id="分片规则（rule）"><a href="#分片规则（rule）" class="headerlink" title="分片规则（rule）"></a>分片规则（rule）</h3><p>一个大表被拆分成多个分片表，就需要一定的规则，按照某种业务逻辑，将数据分到一个确定的分片当中，这个规则就叫做<strong>分片规则</strong>。数据切分选择合适的分片规则非常重要，这将影响到后续的数据处理难度，结合业务，选择合适的分片规则，是对架构师的一个重大考验。对于架构师来说，选择分片规则是一个艰难的，难以抉择的过程。</p>
<h3 id="全局序列号（sequence）"><a href="#全局序列号（sequence）" class="headerlink" title="全局序列号（sequence）"></a>全局序列号（sequence）</h3><p>数据切分后，数据库表中id怎么办？原来在一张表的时候，我们采用id自增，但是数据分布到多个库怎么办？比如：向用户表插入数据，第一条记录插入了用户库1，它的id为1；第二条记录插入了用户库2，如果是自增，它的id也为1。这样id就混乱了，无法确定一条数据的唯一标识了。这时，就需要借助外部的机制保证数据的唯一标识，这种保证数据唯一标识的机制，叫做<strong>全局序列号</strong>。</p>
<h2 id="分库分表概述"><a href="#分库分表概述" class="headerlink" title="分库分表概述"></a>分库分表概述</h2><div class="note info modern"><p>回顾分布式锁：Java已经提供了单体应用锁，使用分布式锁的原因是互联网时代访问量普遍比较高，单体应用已经难以承担如此大的访问量了，所以要部署集群。集群部署后在执行某一种业务的时候可能也需要用到锁，但单体应用锁在一个进程内（Tomcat、JVM）是有效的，在多个进程内是失效的，所以我们就需要使用第三方的组件（数据库、Redis、Zookeeper）实现跨进程的分布式锁。</p>
</div>
<div class="note primary modern"><p>互联网大数据时代，如何解决数据库性能瓶颈；</p>
</div>
<ul>
<li><p>读多写少的情况，采用读写分离；</p>
</li>
<li><p>海量数据的情况，采用数据切分</p>
<ul>
<li>数据切分方式一：垂直切分，按照业务去竖直的切分，订单、商品……，分为不同的数据库</li>
<li>数据切分方式二：水平切分，同一类数据，切分到不同的数据库中</li>
</ul>
</li>
<li><p>实现读写分离和数据切分的两种方式</p>
<ul>
<li><p>模式一：中间层代理（例如：MyCat），由代理层决定操作哪一个数据库。</p>
<p><img src= "" data-lazy-src="中间层代理架构.png" alt=""></p>
</li>
<li><p>模式二：客户端模式（例如Sharding-Jdbc）</p>
<p><img src= "" data-lazy-src="客户端模式架构图.png" alt=""></p>
</li>
</ul>
</li>
</ul>
<h2 id="如何垂直切分、水平切分"><a href="#如何垂直切分、水平切分" class="headerlink" title="如何垂直切分、水平切分"></a>如何垂直切分、水平切分</h2><div class="note info modern"><p>拆分选择：先垂直、后水平：一开始是一个单体数据库，随着业务发展，数据存储量在增高，这个时候要首先选择垂直切分，把业务拆的碎一点，一个数据库只存储一部分业务。如果这样仍然解决不了数据库的压力，那么就需要使用水平拆分了，分片数也可以无限的进行扩展。1、2、4……</p>
</div>
<h3 id="垂直切分"><a href="#垂直切分" class="headerlink" title="垂直切分"></a>垂直切分</h3><ul>
<li>按照业务去切分；</li>
<li>每种业务一个数据库；</li>
<li>不同业务之间，禁止跨库join联查，比如查询订单，一般都是通过服务去查询用户，然后将用户set到订单当中；<img src= "" data-lazy-src="垂直切分架构图.png" alt=""></li>
</ul>
<div class="note primary modern"><p>垂直切分——优点</p>
</div>
<ul>
<li>拆分后业务清晰，拆分规则明确；</li>
<li>系统之间容易扩展和整合；</li>
<li>数据维护简单；</li>
</ul>
<div class="note primary modern"><p>垂直切分——缺点</p>
</div>
<ul>
<li>部分业务表无法join，只能通过接口调用，提升了系统的复杂度；</li>
<li>跨库事务难以处理；</li>
<li>垂直切分后，某些业务数据过于庞大，仍然存在单体性能瓶颈；</li>
</ul>
<h3 id="水平切分"><a href="#水平切分" class="headerlink" title="水平切分"></a>水平切分</h3><ul>
<li><p>将一张表的数据按照某种规则分到不同的数据库中</p>
</li>
<li><p>需确定分片的规则</p>
</li>
<li><p>使用分片字段查询时，可确定实体库，其他字段查询，查询所有库；</p>
<p><img src= "" data-lazy-src="水平切分架构图.png" alt=""></p>
</li>
</ul>
<div class="note primary modern"><p>水平切分——优点</p>
</div>
<ul>
<li>解决了单库大数据、高并发的性能瓶颈；</li>
<li>拆分规则封装好，对应用端几乎透明，开发人员无需关心拆分细节；</li>
<li>提升了系统的稳定性和负载能力；</li>
</ul>
<div class="note primary modern"><p>水平切分——缺点</p>
</div>
<ul>
<li>拆分规则很难抽象；</li>
<li>分片事务一致性难以解决；</li>
<li>二次扩展时，数据迁移、维护难度大；</li>
</ul>
<h2 id="使用MyCat"><a href="#使用MyCat" class="headerlink" title="使用MyCat"></a>使用MyCat</h2><p>MyCat是一个中间代理模式的分库分表的中间件，所有的数据库的请求操作都会发到MyCat上 ，MyCat会把请求操作分配到具体的数据库中操作相应的数据。</p>
<h3 id="操作流程"><a href="#操作流程" class="headerlink" title="操作流程"></a>操作流程</h3><ul>
<li>采用VMware做虚拟机，虚拟出3台Linux CentOS7机器；</li>
<li>采用yum方式，在其中两台安装MySql，并检查两台MySql是否安装正确；</li>
<li>下载MyCat软件包，在第三台机器上安装MyCat，并修改配置文件（分片表、分片规则、分片节点、分片主机等 ）；</li>
</ul>
<p><img src= "" data-lazy-src="MyCat部署架构.png" alt=""></p>
<div class="note info modern"><p>安装 rz 命令用来传输文件;</p>
</div>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">yum -y install lrzsz</span><br></pre></td></tr></table></figure>
<div class="note info modern"><p>防火墙放行指定端口：</p>
</div>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#</span><span class="bash"> 开放3306端口</span></span><br><span class="line">firewall-cmd --zone=public --permanent --add-port=3306/tcp</span><br><span class="line">firewall-cmd --reload</span><br></pre></td></tr></table></figure>
<h3 id="安装MySql"><a href="#安装MySql" class="headerlink" title="安装MySql"></a>安装MySql</h3><div class="note info modern"><p>将两台虚拟机的的ip改为 192.168.0.141 和 192.168.0.141 用来安装MySql</p>
</div>
<ol>
<li><p>下载<a target="_blank" rel="noopener" href="https://dev.mysql.com/get/mysql80-community-release-el7-5.noarch.rpm">yum仓库</a>并上传到192.168.0.141 和 192.168.0.141</p>
</li>
<li><p>安装yum仓库(Adding the MySQL Yum Repository)</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">sudo yum install mysql80-community-release-el7-&#123;version-number&#125;.noarch.rpm</span><br></pre></td></tr></table></figure>
</li>
<li><p>选择一个发行版(Selecting a Release Series)，如果选择当前版本，则跳过这一步；</p>
</li>
<li><p>安装MySql(Installing MySQL)</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">yum install mysql-community-server</span><br></pre></td></tr></table></figure>
</li>
<li><p>启动MySql服务</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">systemctl start mysqld   || service mysqld start</span><br></pre></td></tr></table></figure>
</li>
<li><p>查看进程或者查看启动状态</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">ps -ef | grep mysql    </span><br><span class="line">systemctl status mysqld</span><br></pre></td></tr></table></figure>
</li>
<li><p>查看超级管理员的临时密码</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">grep &#x27;temporary password&#x27; /var/log/mysqld.log</span><br></pre></td></tr></table></figure>
</li>
<li><p>使用临时密码登录并修改密码为<code>Wkq@150227</code></p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">mysql -uroot -p</span><br><span class="line">ALTER USER &#x27;root&#x27;@&#x27;localhost&#x27; IDENTIFIED BY &#x27;MyNewPass4!&#x27;;</span><br><span class="line">flush privileges; </span><br></pre></td></tr></table></figure>
</li>
<li><p>创建新账户并授权</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">create user &#x27;wkq&#x27;@&#x27;%&#x27; identified with mysql_native_password by &#x27;Wkq@150227&#x27;;</span><br><span class="line">grant all on *.* to &#x27;用户名&#x27;@&#x27;可以访问的地址&#x27;;</span><br><span class="line"></span><br><span class="line"><span class="meta">#</span><span class="bash"> 刷新权限</span></span><br><span class="line">flush privileges;</span><br></pre></td></tr></table></figure>
</li>
<li><p>打开navicat进行连接</p>
</li>
</ol>
<h3 id="安装-MyCat"><a href="#安装-MyCat" class="headerlink" title="安装 MyCat"></a>安装 MyCat</h3><ol>
<li><p>下载 <a target="_blank" rel="noopener" href="http://dl.mycat.org.cn/1.6.7.6/20201112144313/Mycat-server-1.6.7.6-release-20201112144313-linux.tar.gz">MyCat 安装包</a>，上传至 Linux 并解压</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">tar -zxvf Mycat-server-1.6.7.6-release-20201112144313-linux.tar.gz</span><br></pre></td></tr></table></figure>
</li>
<li><p>进入 mycat/conf 目录 ，在 server.xml  中配置 MyCat 用户信息</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">user</span> <span class="attr">name</span>=<span class="string">&quot;root&quot;</span> <span class="attr">defaultAccount</span>=<span class="string">&quot;true&quot;</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;password&quot;</span>&gt;</span>123456<span class="tag">&lt;/<span class="name">property</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;schemas&quot;</span>&gt;</span>user<span class="tag">&lt;/<span class="name">property</span>&gt;</span></span><br><span class="line">        </span><br><span class="line">        <span class="comment">&lt;!-- 表级 DML 权限设置 --&gt;</span></span><br><span class="line">        <span class="comment">&lt;!--            </span></span><br><span class="line"><span class="comment">        &lt;privileges check=&quot;false&quot;&gt;</span></span><br><span class="line"><span class="comment">                &lt;schema name=&quot;TESTDB&quot; dml=&quot;0110&quot; &gt;</span></span><br><span class="line"><span class="comment">                        &lt;table name=&quot;tb01&quot; dml=&quot;0000&quot;&gt;&lt;/table&gt;</span></span><br><span class="line"><span class="comment">                        &lt;table name=&quot;tb02&quot; dml=&quot;1111&quot;&gt;&lt;/table&gt;</span></span><br><span class="line"><span class="comment">                &lt;/schema&gt;</span></span><br><span class="line"><span class="comment">        &lt;/privileges&gt;           </span></span><br><span class="line"><span class="comment">         --&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">user</span>&gt;</span></span><br><span class="line">   </span><br><span class="line"><span class="tag">&lt;<span class="name">user</span> <span class="attr">name</span>=<span class="string">&quot;user&quot;</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;password&quot;</span>&gt;</span>user<span class="tag">&lt;/<span class="name">property</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;schemas&quot;</span>&gt;</span>user<span class="tag">&lt;/<span class="name">property</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;readOnly&quot;</span>&gt;</span>true<span class="tag">&lt;/<span class="name">property</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">user</span>&gt;</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>在 schema.xml 中配置 数据库 信息</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;?xml version=&quot;1.0&quot;?&gt;</span></span><br><span class="line"><span class="meta">&lt;!DOCTYPE <span class="meta-keyword">mycat</span>:schema <span class="meta-keyword">SYSTEM</span> <span class="meta-string">&quot;schema.dtd&quot;</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">mycat:schema</span> <span class="attr">xmlns:mycat</span>=<span class="string">&quot;http://io.mycat/&quot;</span>&gt;</span></span><br><span class="line"></span><br><span class="line">        <span class="tag">&lt;<span class="name">schema</span> <span class="attr">name</span>=<span class="string">&quot;user&quot;</span> <span class="attr">checkSQLschema</span>=<span class="string">&quot;true&quot;</span> <span class="attr">sqlMaxLimit</span>=<span class="string">&quot;100&quot;</span>&gt;</span></span><br><span class="line">                <span class="comment">&lt;!-- auto sharding by id (long) --&gt;</span></span><br><span class="line">                <span class="tag">&lt;<span class="name">table</span> <span class="attr">name</span>=<span class="string">&quot;user&quot;</span> <span class="attr">dataNode</span>=<span class="string">&quot;dn241,dn242&quot;</span> <span class="attr">rule</span>=<span class="string">&quot;auto-sharding-long&quot;</span> /&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">schema</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">dataNode</span> <span class="attr">name</span>=<span class="string">&quot;dn241&quot;</span> <span class="attr">dataHost</span>=<span class="string">&quot;dh241&quot;</span> <span class="attr">database</span>=<span class="string">&quot;user_241&quot;</span> /&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">dataNode</span> <span class="attr">name</span>=<span class="string">&quot;dn242&quot;</span> <span class="attr">dataHost</span>=<span class="string">&quot;dh242&quot;</span> <span class="attr">database</span>=<span class="string">&quot;user_242&quot;</span> /&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">dataHost</span> <span class="attr">name</span>=<span class="string">&quot;dh241&quot;</span> <span class="attr">maxCon</span>=<span class="string">&quot;1000&quot;</span> <span class="attr">minCon</span>=<span class="string">&quot;10&quot;</span> <span class="attr">balance</span>=<span class="string">&quot;0&quot;</span></span></span><br><span class="line"><span class="tag">                          <span class="attr">writeType</span>=<span class="string">&quot;0&quot;</span> <span class="attr">dbType</span>=<span class="string">&quot;mysql&quot;</span> <span class="attr">dbDriver</span>=<span class="string">&quot;native&quot;</span> <span class="attr">switchType</span>=<span class="string">&quot;1&quot;</span>  <span class="attr">slaveThreshold</span>=<span class="string">&quot;100&quot;</span>&gt;</span></span><br><span class="line">                <span class="tag">&lt;<span class="name">heartbeat</span>&gt;</span>select user()<span class="tag">&lt;/<span class="name">heartbeat</span>&gt;</span></span><br><span class="line">                <span class="tag">&lt;<span class="name">writeHost</span> <span class="attr">host</span>=<span class="string">&quot;M1&quot;</span> <span class="attr">url</span>=<span class="string">&quot;192.168.0.241:3306&quot;</span> <span class="attr">user</span>=<span class="string">&quot;wkq&quot;</span></span></span><br><span class="line"><span class="tag">                                   <span class="attr">password</span>=<span class="string">&quot;Wkq@150227&quot;</span>&gt;</span></span><br><span class="line">                <span class="tag">&lt;/<span class="name">writeHost</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">dataHost</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">dataHost</span> <span class="attr">name</span>=<span class="string">&quot;dh242&quot;</span> <span class="attr">maxCon</span>=<span class="string">&quot;1000&quot;</span> <span class="attr">minCon</span>=<span class="string">&quot;10&quot;</span> <span class="attr">balance</span>=<span class="string">&quot;0&quot;</span></span></span><br><span class="line"><span class="tag">                          <span class="attr">writeType</span>=<span class="string">&quot;0&quot;</span> <span class="attr">dbType</span>=<span class="string">&quot;mysql&quot;</span> <span class="attr">dbDriver</span>=<span class="string">&quot;native&quot;</span> <span class="attr">switchType</span>=<span class="string">&quot;1&quot;</span>  <span class="attr">slaveThreshold</span>=<span class="string">&quot;100&quot;</span>&gt;</span></span><br><span class="line">                <span class="tag">&lt;<span class="name">heartbeat</span>&gt;</span>select user()<span class="tag">&lt;/<span class="name">heartbeat</span>&gt;</span></span><br><span class="line">                <span class="tag">&lt;<span class="name">writeHost</span> <span class="attr">host</span>=<span class="string">&quot;M1&quot;</span> <span class="attr">url</span>=<span class="string">&quot;192.168.0.242:3306&quot;</span> <span class="attr">user</span>=<span class="string">&quot;wkq&quot;</span></span></span><br><span class="line"><span class="tag">                                   <span class="attr">password</span>=<span class="string">&quot;Wkq@150227&quot;</span>&gt;</span></span><br><span class="line">                <span class="tag">&lt;/<span class="name">writeHost</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">dataHost</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">mycat:schema</span>&gt;</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>修改 auto-sharding-long 规则</p>
<figure class="highlight tex"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"># range start-end ,data node index</span><br><span class="line"># K&#x3D;1000,M&#x3D;10000.</span><br><span class="line">0-500M&#x3D;0</span><br><span class="line">500M-1000M&#x3D;1</span><br><span class="line"># 1000M-1500M&#x3D;2</span><br></pre></td></tr></table></figure>
</li>
<li><p>启动 MyCat，并使用 navicat 连接</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">./bin/mycat start</span><br></pre></td></tr></table></figure>
</li>
</ol>
<h3 id="server-xml"><a href="#server-xml" class="headerlink" title="server.xml"></a>server.xml</h3><div class="note info modern"><p>MyCat启动的时候有两个端口，一个是 8066 ，就是连接的这个端口，就是用来做数据库的操作（增删改查） ；还有一个是 9066 端口，是 MyCat 的管理端口，<code>reload @@config;</code> 重载基础配置文件。<code>reload @@config_all</code> 重载所有配置文件。如果修改的是数据源相关的配置，修改的时候要是<code>reload @@config_all</code> 重载配置文件。</p>
</div>
<ul>
<li>配置 MyCat 的用户名、密码、权限、Schema 等    </li>
<li>如同给 MySql 新建用户一样</li>
<li>客户端连接 MyCat 与连接 MySql 无异</li>
</ul>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">user</span> <span class="attr">name</span>=<span class="string">&quot;root&quot;</span> <span class="attr">defaultAccount</span>=<span class="string">&quot;true&quot;</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;password&quot;</span>&gt;</span>123456<span class="tag">&lt;/<span class="name">property</span>&gt;</span></span><br><span class="line">    	<span class="comment">&lt;!-- 必须和 schema.xml 中的 schema 名称对应起来，如果有多个 schema 就用 ， 隔开 ; --&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;schemas&quot;</span>&gt;</span>user,order,product<span class="tag">&lt;/<span class="name">property</span>&gt;</span></span><br><span class="line">        <span class="comment">&lt;!-- 上面的配置在 schema.xml 中需要对应配置 3 个 schema 标签  --&gt;</span></span><br><span class="line">    </span><br><span class="line">    </span><br><span class="line">        <span class="comment">&lt;!-- 表级 DML 权限设置 --&gt;</span></span><br><span class="line">        <span class="comment">&lt;!--            </span></span><br><span class="line"><span class="comment">        &lt;privileges check=&quot;false&quot;&gt;</span></span><br><span class="line"><span class="comment">                &lt;schema name=&quot;TESTDB&quot; dml=&quot;0110&quot; &gt;</span></span><br><span class="line"><span class="comment">                        &lt;table name=&quot;tb01&quot; dml=&quot;0000&quot;&gt;&lt;/table&gt;</span></span><br><span class="line"><span class="comment">                        &lt;table name=&quot;tb02&quot; dml=&quot;1111&quot;&gt;&lt;/table&gt;</span></span><br><span class="line"><span class="comment">                &lt;/schema&gt;</span></span><br><span class="line"><span class="comment">        &lt;/privileges&gt;           </span></span><br><span class="line"><span class="comment">         --&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">user</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">user</span> <span class="attr">name</span>=<span class="string">&quot;user&quot;</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;password&quot;</span>&gt;</span>user<span class="tag">&lt;/<span class="name">property</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;schemas&quot;</span>&gt;</span>user<span class="tag">&lt;/<span class="name">property</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;readOnly&quot;</span>&gt;</span>true<span class="tag">&lt;/<span class="name">property</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">user</span>&gt;</span></span><br></pre></td></tr></table></figure>
<h3 id="schema-xml"><a href="#schema-xml" class="headerlink" title="schema.xml"></a>schema.xml</h3><ul>
<li>配置 dataHost（节点主机），包括读 host、写 host，用来连接实际的 MySql 数据库；</li>
<li>配置 dataNode （数据节点），在 dataHost 的基础上进行配置 ， 指定到具体的 dataHost 和数据库；</li>
<li>配置 schema ，表名、数据节点、分片规则等；</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line">&lt;?xml version&#x3D;&quot;1.0&quot;?&gt;</span><br><span class="line">&lt;!DOCTYPE mycat:schema SYSTEM &quot;schema.dtd&quot;&gt;</span><br><span class="line">&lt;mycat:schema xmlns:mycat&#x3D;&quot;http:&#x2F;&#x2F;io.mycat&#x2F;&quot;&gt;</span><br><span class="line"></span><br><span class="line">        &lt;schema name&#x3D;&quot;user&quot; checkSQLschema&#x3D;&quot;true&quot; sqlMaxLimit&#x3D;&quot;100&quot;&gt;</span><br><span class="line">                &lt;!-- auto sharding by id (long) --&gt;</span><br><span class="line">                &lt;table name&#x3D;&quot;user&quot; dataNode&#x3D;&quot;dn241,dn242&quot; rule&#x3D;&quot;auto-sharding-long&quot; &#x2F;&gt;</span><br><span class="line">        &lt;&#x2F;schema&gt;</span><br><span class="line">        &lt;dataNode name&#x3D;&quot;dn241&quot; dataHost&#x3D;&quot;dh241&quot; database&#x3D;&quot;user_241&quot; &#x2F;&gt;</span><br><span class="line">        &lt;dataNode name&#x3D;&quot;dn242&quot; dataHost&#x3D;&quot;dh242&quot; database&#x3D;&quot;user_242&quot; &#x2F;&gt;</span><br><span class="line">        &lt;dataHost name&#x3D;&quot;dh241&quot; maxCon&#x3D;&quot;1000&quot; minCon&#x3D;&quot;10&quot; balance&#x3D;&quot;0&quot;</span><br><span class="line">                          writeType&#x3D;&quot;0&quot; dbType&#x3D;&quot;mysql&quot; dbDriver&#x3D;&quot;native&quot; switchType&#x3D;&quot;1&quot;  slaveThreshold&#x3D;&quot;100&quot;&gt;</span><br><span class="line">                &lt;heartbeat&gt;select user()&lt;&#x2F;heartbeat&gt;</span><br><span class="line">                &lt;writeHost host&#x3D;&quot;M1&quot; url&#x3D;&quot;192.168.0.241:3306&quot; user&#x3D;&quot;wkq&quot;</span><br><span class="line">                                   password&#x3D;&quot;Wkq@150227&quot;&gt;</span><br><span class="line">                &lt;&#x2F;writeHost&gt;</span><br><span class="line">        &lt;&#x2F;dataHost&gt;</span><br><span class="line">        &lt;dataHost name&#x3D;&quot;dh242&quot; maxCon&#x3D;&quot;1000&quot; minCon&#x3D;&quot;10&quot; balance&#x3D;&quot;0&quot;</span><br><span class="line">                          writeType&#x3D;&quot;0&quot; dbType&#x3D;&quot;mysql&quot; dbDriver&#x3D;&quot;native&quot; switchType&#x3D;&quot;1&quot;  slaveThreshold&#x3D;&quot;100&quot;&gt;</span><br><span class="line">                &lt;heartbeat&gt;select user()&lt;&#x2F;heartbeat&gt;</span><br><span class="line">                &lt;writeHost host&#x3D;&quot;M1&quot; url&#x3D;&quot;192.168.0.242:3306&quot; user&#x3D;&quot;wkq&quot;</span><br><span class="line">                                   password&#x3D;&quot;Wkq@150227&quot;&gt;</span><br><span class="line">                &lt;&#x2F;writeHost&gt;</span><br><span class="line">        &lt;&#x2F;dataHost&gt;</span><br><span class="line">&lt;&#x2F;mycat:schema&gt;</span><br></pre></td></tr></table></figure>
<div class="note info modern"><p>dataHost :</p>
</div>
<ul>
<li><p>balance : 负载均衡类型（针对读请求）：</p>
<ul>
<li>balance=’0’：不开启读写分离机制，所有的读操作读发送到当前可用的 writeHost上</li>
<li>balance=’1’ : 全部的 readHost 与 stand by writeHost 参与 select  语句的负载均衡，简单的说，当双柱双从模式（ M1 -&gt; S1 , M2 -&gt; S2 ,并且 M1 与 M2 互为主备），正常情况下，M2，S1，S2 都参与 select 语句的负载均衡。</li>
<li>balance=”2” : 所有的读操作读随机的在 writeHost、readHost上分发</li>
<li>balance=”3“ ： 所有的读请求随机的分发到 writeHost 对应的 readHost 执行，writeHost 不承担读压力。</li>
</ul>
</li>
<li><p>writeType：定义写请求落在哪一个写库上，前提条件是配置了多个写库。0 表示所有的写请求会落到第一个 writeHost上，当第一个 writeHost 不可用时，写请求才会落到第二个 readHost上 ；1 随机 ，已废弃；</p>
</li>
<li>maxCon ：最大连接数</li>
<li>minCon ： 最小连接数</li>
<li>dbType ： 数据库类型</li>
<li>dbDriver ：驱动</li>
<li>writeHost ： 写库的连接信息，可以配置多个。具体的规则由 writeType 定义<ul>
<li>readHost :  可以配置多个读库，一个主可以对应多个从</li>
</ul>
</li>
</ul>
<div class="note info modern"><p>dataNode : </p>
</div>
<ul>
<li>dataHost ： 节点主机；</li>
<li>database : 具体的节点主机中的数据库；</li>
</ul>
<div class="note info modern"><p>schema : </p>
</div>
<ul>
<li>name： 要和 server.xml 中的 schema 对应起来；</li>
<li>checkSQLschema : 是否去掉 SQL 中的 Schema；</li>
<li><font style = "color:red">sqlMaxLimit </font>： select 默认的 limit值，仅对分片表有效；</li>
</ul>
<div class="note info modern"><p>table : 定义表</p>
</div>
<ul>
<li>name :  定义逻辑表的表名</li>
<li>dataNode ： 定义逻辑表的数据节点</li>
<li><font style = "color:red">rule</font> ：定义分片表的分片规则，必须与 rule.xml 中的 tableRule 对应；</li>
<li>ruleRequired : 是否绑定分片规则，如果为 true ，没有绑定分片规则，程序报错；</li>
</ul>
<h3 id="rule-xml"><a href="#rule-xml" class="headerlink" title="rule.xml"></a>rule.xml</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"> &lt;tableRule name&#x3D;&quot;auto-sharding-long&quot;&gt;</span><br><span class="line">               &lt;rule&gt;</span><br><span class="line">                       &lt;columns&gt;id&lt;&#x2F;columns&gt;</span><br><span class="line">                       &lt;algorithm&gt;rang-long&lt;&#x2F;algorithm&gt;</span><br><span class="line">               &lt;&#x2F;rule&gt;</span><br><span class="line">  &lt;&#x2F;tableRule&gt;</span><br><span class="line">&lt;function name&#x3D;&quot;rang-long&quot;</span><br><span class="line">               class&#x3D;&quot;io.mycat.route.function.AutoPartitionByLong&quot;&gt;</span><br><span class="line">               &lt;property name&#x3D;&quot;mapFile&quot;&gt;autopartition-long.txt&lt;&#x2F;property&gt;</span><br><span class="line">       &lt;&#x2F;function&gt;</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<ul>
<li>必须和 schema.xml &gt; schmea &gt; table &gt; rule 的值必须填写 tableRule 中的 name </li>
<li>column 表示按那一列进行分片</li>
<li>algorithm 表示分片的逻辑 ，可用在文件的后半部分的 function 标签中找到，funcation 标签对应分片逻辑<ul>
<li>class 标志着分片规则的实现类</li>
<li>property 表示映射的文件</li>
</ul>
</li>
</ul>
<h2 id="MySql-主从"><a href="#MySql-主从" class="headerlink" title="MySql 主从"></a>MySql 主从</h2><ul>
<li><p>主配置 log- bin ,指定文件的名字；</p>
</li>
<li><p>主配置 server-id ,默认为 1；</p>
</li>
<li><p>从配置 server-id , 与主不能重复；</p>
</li>
<li><p>主创建备份账户并授权 <code>REPLICATION SLAVE</code>，这个账户用于主存数据库之间的同步；</p>
</li>
<li><p>主进行锁表（锁表是为了主存之间的数据备份,让所有的写操作不会落到数据库上，同步之后的数据通过 log-bin 进行同步)  <code>FLUSH TABLES WITH READ LOCK</code>;</p>
</li>
<li><p>主找到 log-bin 的位置 ， 输入 <code>SHOW MASTER STATUS</code> 查询;</p>
</li>
<li><p>主备份数据 <code>mysqldump --all-databases --master-data &gt; dumpdb.db</code>;</p>
</li>
<li><p>主进行解锁 <code>UNLOCK TABLES</code> ;</p>
</li>
<li><p>从导入 dump 的数据；</p>
</li>
<li><p>在从上设置主的配置  </p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">CHANGE MASTER TO </span><br><span class="line">MASTER_HOST = &#x27;master_host_name&#x27;,</span><br><span class="line">MASTER_USER = &#x27;replication_user_name&#x27;,</span><br><span class="line">MASTER_PASSWORD = &#x27;replication_password&#x27;,</span><br><span class="line">MASTER_LOG_FILE = &#x27;recorded_log_file_name&#x27;,</span><br><span class="line">MASTER_LOG_POS = &#x27;recorded_log_position&#x27;;</span><br></pre></td></tr></table></figure>
</li>
<li><p>从执行 <code>START SLAVE</code>;</p>
</li>
<li></li>
</ul>
<div class="note info modern"><p>主库配置：</p>
</div>
<ol>
<li><p>打开 /etc/my.cnf 在最后面追加两行；</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">log-bin=wkq_mysql</span><br><span class="line">server-id=1</span><br></pre></td></tr></table></figure>
</li>
<li><p>重启 mysql </p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">systemctl restart mysqld</span><br></pre></td></tr></table></figure>
</li>
<li><p>登录、创建用户并授权(<code>*.*</code>表示任意库任意表)、刷新权限</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">mysql -uroot -pWkq@150227;</span><br><span class="line">create user &#x27;repl&#x27;@&#x27;%&#x27; identified by &#x27;Wkq@150227&#x27;;</span><br><span class="line">grant replication slave on *.* to &#x27;repl&#x27;@&#x27;%&#x27;;</span><br><span class="line">flush privileges; </span><br></pre></td></tr></table></figure>
</li>
<li><p>锁表操作</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">FLUSH TABLES WITH READ LOCK;</span><br></pre></td></tr></table></figure>
</li>
<li><p>找到 log-bin 的位置，如下图位置是 858，需要将其配置到从库中</p>
<p><img src= "" data-lazy-src="主log-bin的位置.png" alt=""></p>
</li>
<li><p>主备份数据，这条语句必须在 mysql 之外执行。</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">mysqldump --all-databases --master-data &gt; dumpdb.db -uroot -pWkq@150227</span><br></pre></td></tr></table></figure>
</li>
<li><p>将 dumpdb.db 文件复制到从库</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">scp dumpdb.db root@192.168.0.243:/root/</span><br></pre></td></tr></table></figure>
</li>
<li><p>从库加载 dumpdb.db 文件到 mysql 中</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">mysql &lt; dumpdb.db -uroot -p</span><br><span class="line"><span class="meta">#</span><span class="bash"> 注意，在8.0.25版本以上需要注释掉dumpdb.db文件中第一行才能加载成功</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>同步结束后、解除锁表</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">unlock tables;</span><br></pre></td></tr></table></figure>
</li>
</ol>
<div class="note info modern"><p>从库配置：</p>
</div>
<ol>
<li><p>打开 /etc/my.cnf 在最后面追加一行；并重启</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">server-id=2</span><br></pre></td></tr></table></figure>
</li>
<li><p>在从上设置主的配置</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">CHANGE MASTER TO </span><br><span class="line">MASTER_HOST = &#x27;192.168.0.241&#x27;,</span><br><span class="line">MASTER_USER = &#x27;rep&#x27;,</span><br><span class="line">MASTER_PASSWORD = &#x27;Rep@150227&#x27;,</span><br><span class="line">MASTER_LOG_FILE = &#x27;mysql-bin.000003&#x27;,</span><br><span class="line">MASTER_LOG_POS = 658;</span><br></pre></td></tr></table></figure>
</li>
<li><p>从执行 <code>START SLAVE</code>;</p>
</li>
<li></li>
</ol>
</article><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/%E8%AF%BB%E5%86%99%E5%88%86%E7%A6%BB/">读写分离</a><a class="post-meta__tags" href="/tags/%E5%88%86%E5%BA%93%E5%88%86%E8%A1%A8/">分库分表</a><a class="post-meta__tags" href="/tags/%E5%88%86%E5%B8%83%E5%BC%8F%E6%95%B0%E6%8D%AE%E5%BA%93%E7%B3%BB%E7%BB%9F/">分布式数据库系统</a></div><div class="post_share"><div class="social-share" data-image="/media/top_img.jpg" data-sites="wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/social-share.js/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/social-share.js/dist/js/social-share.min.js" defer></script></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src= "" data-lazy-src="/media/avatar.jpg" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">王开琦</div><div class="author-info__description">今日事今日毕</div></div><div class="card-info-data"><div class="card-info-data-item is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">15</div></a></div><div class="card-info-data-item is-center"><a href="/tags/"><div class="headline">标签</div><div class="length-num">17</div></a></div><div class="card-info-data-item is-center"><a href="/categories/"><div class="headline">分类</div><div class="length-num">15</div></a></div></div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#MyCat%E6%A6%82%E8%BF%B0%E4%B8%8E%E5%9F%BA%E6%9C%AC%E6%A6%82%E5%BF%B5"><span class="toc-number">1.</span> <span class="toc-text">MyCat概述与基本概念</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#MyCat%E7%9A%84%E5%BA%94%E7%94%A8%E5%9C%BA%E6%99%AF"><span class="toc-number">2.</span> <span class="toc-text">MyCat的应用场景</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#MyCat%E4%B8%AD%E7%9A%84%E5%9F%BA%E6%9C%AC%E6%A6%82%E5%BF%B5"><span class="toc-number">3.</span> <span class="toc-text">MyCat中的基本概念</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E9%80%BB%E8%BE%91%E5%BA%93%EF%BC%88Schema%EF%BC%89"><span class="toc-number">3.1.</span> <span class="toc-text">逻辑库（Schema）</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E9%80%BB%E8%BE%91%E8%A1%A8%EF%BC%88table%EF%BC%89"><span class="toc-number">3.2.</span> <span class="toc-text">逻辑表（table）</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%88%86%E7%89%87%E8%8A%82%E7%82%B9%EF%BC%88dataNode%EF%BC%89"><span class="toc-number">3.3.</span> <span class="toc-text">分片节点（dataNode）</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%8A%82%E7%82%B9%E4%B8%BB%E6%9C%BA%EF%BC%88dataHost%EF%BC%89"><span class="toc-number">3.4.</span> <span class="toc-text">节点主机（dataHost）</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%88%86%E7%89%87%E8%A7%84%E5%88%99%EF%BC%88rule%EF%BC%89"><span class="toc-number">3.5.</span> <span class="toc-text">分片规则（rule）</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%85%A8%E5%B1%80%E5%BA%8F%E5%88%97%E5%8F%B7%EF%BC%88sequence%EF%BC%89"><span class="toc-number">3.6.</span> <span class="toc-text">全局序列号（sequence）</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%88%86%E5%BA%93%E5%88%86%E8%A1%A8%E6%A6%82%E8%BF%B0"><span class="toc-number">4.</span> <span class="toc-text">分库分表概述</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%A6%82%E4%BD%95%E5%9E%82%E7%9B%B4%E5%88%87%E5%88%86%E3%80%81%E6%B0%B4%E5%B9%B3%E5%88%87%E5%88%86"><span class="toc-number">5.</span> <span class="toc-text">如何垂直切分、水平切分</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%9E%82%E7%9B%B4%E5%88%87%E5%88%86"><span class="toc-number">5.1.</span> <span class="toc-text">垂直切分</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%B0%B4%E5%B9%B3%E5%88%87%E5%88%86"><span class="toc-number">5.2.</span> <span class="toc-text">水平切分</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BD%BF%E7%94%A8MyCat"><span class="toc-number">6.</span> <span class="toc-text">使用MyCat</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%93%8D%E4%BD%9C%E6%B5%81%E7%A8%8B"><span class="toc-number">6.1.</span> <span class="toc-text">操作流程</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AE%89%E8%A3%85MySql"><span class="toc-number">6.2.</span> <span class="toc-text">安装MySql</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AE%89%E8%A3%85-MyCat"><span class="toc-number">6.3.</span> <span class="toc-text">安装 MyCat</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#server-xml"><span class="toc-number">6.4.</span> <span class="toc-text">server.xml</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#schema-xml"><span class="toc-number">6.5.</span> <span class="toc-text">schema.xml</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#rule-xml"><span class="toc-number">6.6.</span> <span class="toc-text">rule.xml</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#MySql-%E4%B8%BB%E4%BB%8E"><span class="toc-number">7.</span> <span class="toc-text">MySql 主从</span></a></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/blog/3-%E6%95%B0%E6%8D%AE%E5%BA%93-%E6%95%B0%E6%8D%AE%E5%BA%93%E7%9B%91%E6%8E%A7%E4%B8%8E%E8%B0%83%E4%BC%98-%E7%89%B9%E5%AE%9A%E8%AF%AD%E5%8F%A5%E7%9A%84%E5%8E%9F%E7%90%86%E5%92%8C%E8%B0%83%E4%BC%98/" title="3.数据库-数据库监控与调优-特定语句的原理和调优"><img src= "" data-lazy-src="/media/top_img.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="3.数据库-数据库监控与调优-特定语句的原理和调优"/></a><div class="content"><a class="title" href="/blog/3-%E6%95%B0%E6%8D%AE%E5%BA%93-%E6%95%B0%E6%8D%AE%E5%BA%93%E7%9B%91%E6%8E%A7%E4%B8%8E%E8%B0%83%E4%BC%98-%E7%89%B9%E5%AE%9A%E8%AF%AD%E5%8F%A5%E7%9A%84%E5%8E%9F%E7%90%86%E5%92%8C%E8%B0%83%E4%BC%98/" title="3.数据库-数据库监控与调优-特定语句的原理和调优">3.数据库-数据库监控与调优-特定语句的原理和调优</a><time datetime="2022-04-22T08:14:26.000Z" title="发表于 2022-04-22 16:14:26">2022-04-22</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/blog/3-%E6%95%B0%E6%8D%AE%E5%BA%93-%E6%95%B0%E6%8D%AE%E5%BA%93%E7%9B%91%E6%8E%A7%E4%B8%8E%E8%B0%83%E4%BC%98-%E6%95%B0%E6%8D%AE%E5%BA%93%E8%B0%83%E4%BC%98%E7%90%86%E8%AE%BA/" title="数据库调优理论"><img src= "" data-lazy-src="/media/top_img.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="数据库调优理论"/></a><div class="content"><a class="title" href="/blog/3-%E6%95%B0%E6%8D%AE%E5%BA%93-%E6%95%B0%E6%8D%AE%E5%BA%93%E7%9B%91%E6%8E%A7%E4%B8%8E%E8%B0%83%E4%BC%98-%E6%95%B0%E6%8D%AE%E5%BA%93%E8%B0%83%E4%BC%98%E7%90%86%E8%AE%BA/" title="数据库调优理论">数据库调优理论</a><time datetime="2022-04-22T08:13:31.000Z" title="发表于 2022-04-22 16:13:31">2022-04-22</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/blog/3-%E6%95%B0%E6%8D%AE%E5%BA%93-%E6%95%B0%E6%8D%AE%E5%BA%93%E7%9B%91%E6%8E%A7%E4%B8%8E%E8%B0%83%E4%BC%98-%E6%85%A2%E6%9F%A5%E8%AF%A2%E5%8F%91%E7%8E%B0%E4%B8%8E%E5%88%86%E6%9E%90/" title="慢查询发现与分析"><img src= "" data-lazy-src="/media/top_img.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="慢查询发现与分析"/></a><div class="content"><a class="title" href="/blog/3-%E6%95%B0%E6%8D%AE%E5%BA%93-%E6%95%B0%E6%8D%AE%E5%BA%93%E7%9B%91%E6%8E%A7%E4%B8%8E%E8%B0%83%E4%BC%98-%E6%85%A2%E6%9F%A5%E8%AF%A2%E5%8F%91%E7%8E%B0%E4%B8%8E%E5%88%86%E6%9E%90/" title="慢查询发现与分析">慢查询发现与分析</a><time datetime="2022-04-22T08:12:32.000Z" title="发表于 2022-04-22 16:12:32">2022-04-22</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/blog/1-Java-JVM%E8%B0%83%E4%BC%98%E7%90%86%E8%AE%BA/" title="JVM 调优理论"><img src= "" data-lazy-src="/media/top_img.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="JVM 调优理论"/></a><div class="content"><a class="title" href="/blog/1-Java-JVM%E8%B0%83%E4%BC%98%E7%90%86%E8%AE%BA/" title="JVM 调优理论">JVM 调优理论</a><time datetime="2022-04-21T06:40:41.000Z" title="发表于 2022-04-21 14:40:41">2022-04-21</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/blog/20-%E5%BE%AE%E6%9C%8D%E5%8A%A1%E6%9E%B6%E6%9E%84-%E6%9C%8D%E5%8A%A1%E6%B2%BB%E7%90%86-%E6%9C%8D%E5%8A%A1%E6%B2%BB%E7%90%86Eureka/" title="服务治理Eureka"><img src= "" data-lazy-src="/media/top_img.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="服务治理Eureka"/></a><div class="content"><a class="title" href="/blog/20-%E5%BE%AE%E6%9C%8D%E5%8A%A1%E6%9E%B6%E6%9E%84-%E6%9C%8D%E5%8A%A1%E6%B2%BB%E7%90%86-%E6%9C%8D%E5%8A%A1%E6%B2%BB%E7%90%86Eureka/" title="服务治理Eureka">服务治理Eureka</a><time datetime="2022-04-13T13:18:27.000Z" title="发表于 2022-04-13 21:18:27">2022-04-13</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="footer_custom_text">Copyright© 2022 blog.wkq.pub <span>陇ICP备2021003596号</span></a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="translateLink" type="button" title="简繁转换">繁</button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div id="local-search"><div class="search-dialog"><div class="search-dialog__title" id="local-search-title">本地搜索</div><div id="local-input-panel"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章" type="text"/></div></div></div><hr/><div id="local-search-results"></div><span class="search-close-button"><i class="fas fa-times"></i></span></div><div id="search-mask"></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="/js/tw_cn.js"></script><script src="https://cdn.jsdelivr.net/npm/vanilla-lazyload/dist/lazyload.iife.min.js"></script><script src="/js/search/local-search.js"></script><script>var preloader = {
  endLoading: () => {
    document.body.style.overflow = 'auto';
    document.getElementById('loading-box').classList.add("loaded")
  },
  initLoading: () => {
    document.body.style.overflow = '';
    document.getElementById('loading-box').classList.remove("loaded")

  }
}
window.addEventListener('load',preloader.endLoading())</script><div class="js-pjax"><script>if (document.getElementsByClassName('mermaid').length) {
  if (window.mermaidJsLoad) mermaid.init()
  else {
    getScript('https://cdn.jsdelivr.net/npm/mermaid/dist/mermaid.min.js').then(() => {
      window.mermaidJsLoad = true
      mermaid.initialize({
        theme: 'default',
      })
      true && mermaid.init()
    })
  }
}</script></div><div class="aplayer no-destroy" data-id="7675104902" data-server="tencent" data-type="playlist" data-fixed="true" data-mini="true" data-listFolded="false" data-order="random" data-preload="none" data-autoplay="true" muted></div><canvas class="fireworks" mobile="true"></canvas><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/fireworks.min.js"></script><script defer="defer" id="fluttering_ribbon" mobile="false" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/canvas-fluttering-ribbon.min.js"></script><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/aplayer/dist/APlayer.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/aplayer/dist/APlayer.min.js"></script><script src="https://cdn.jsdelivr.net/gh/metowolf/MetingJS@1.2/dist/Meting.min.js"></script><script src="https://cdn.jsdelivr.net/npm/pjax/pjax.min.js"></script><script>let pjaxSelectors = [
  'title',
  '#config-diff',
  '#body-wrap',
  '#rightside-config-hide',
  '#rightside-config-show',
  '.js-pjax'
]

if (false) {
  pjaxSelectors.unshift('meta[property="og:image"]', 'meta[property="og:title"]', 'meta[property="og:url"]')
}

var pjax = new Pjax({
  elements: 'a:not([target="_blank"])',
  selectors: pjaxSelectors,
  cacheBust: false,
  analytics: false,
  scrollRestoration: false
})

document.addEventListener('pjax:send', function () {

  // removeEventListener scroll 
  window.removeEventListener('scroll', window.tocScrollFn)
  window.removeEventListener('scroll', scrollCollect)

  typeof preloader === 'object' && preloader.initLoading()
  
  if (window.aplayers) {
    for (let i = 0; i < window.aplayers.length; i++) {
      if (!window.aplayers[i].options.fixed) {
        window.aplayers[i].destroy()
      }
    }
  }

  typeof typed === 'object' && typed.destroy()

  //reset readmode
  const $bodyClassList = document.body.classList
  $bodyClassList.contains('read-mode') && $bodyClassList.remove('read-mode')

})

document.addEventListener('pjax:complete', function () {
  window.refreshFn()

  document.querySelectorAll('script[data-pjax]').forEach(item => {
    const newScript = document.createElement('script')
    const content = item.text || item.textContent || item.innerHTML || ""
    Array.from(item.attributes).forEach(attr => newScript.setAttribute(attr.name, attr.value))
    newScript.appendChild(document.createTextNode(content))
    item.parentNode.replaceChild(newScript, item)
  })

  GLOBAL_CONFIG.islazyload && window.lazyLoadInstance.update()

  typeof chatBtnFn === 'function' && chatBtnFn()
  typeof panguInit === 'function' && panguInit()

  // google analytics
  typeof gtag === 'function' && gtag('config', '', {'page_path': window.location.pathname});

  // baidu analytics
  typeof _hmt === 'object' && _hmt.push(['_trackPageview',window.location.pathname]);

  typeof loadMeting === 'function' && document.getElementsByClassName('aplayer').length && loadMeting()

  // Analytics
  if (false) {
    MtaH5.pgv()
  }

  // prismjs
  typeof Prism === 'object' && Prism.highlightAll()

  typeof preloader === 'object' && preloader.endLoading()
})

document.addEventListener('pjax:error', (e) => {
  if (e.request.status === 404) {
    pjax.loadUrl('/404.html')
  }
})</script></div><script src="/live2d_models/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"pluginRootPath":"live2d_models/","pluginJsPath":"lib/","pluginModelPath":"assets/","tagMode":false,"debug":false,"model":{"jsonPath":"/live2d_models/assets/penchan.model.json"},"display":{"position":"left","width":100,"height":150,"hOffset":-10,"vOffset":40},"mobile":{"show":true},"react":{"opacity":0.7},"log":false});</script></body></html>